###################################################################################
####State Capacity, Insurgency, and Civil War - District Level Robustness (Sum)####
###################################################################################
library(MASS) 
library(pscl)
library(foreign)
library(Hmisc)
library(rgdal)
library(RArcInfo)
library(stargazer)
library(mvtnorm)
library(Zelig)
library(readstata13)
library(pROC)
library(robust)
library(cvTools)
library(boot)
library(maps)
library(mapdata)
library(doBy)
library(raster)
library(survival)
library(spduration)
library(DAMisc)
library(plyr)
library(DataCombine)
library(plm)
library(AER)
library(lmtest)

########################
###Create The Dataset###
########################
###Importing the main data
#set working directory to location of dta file
setwd("~/Google Drive/NSLC/Final Dofiles/")
#Import the data
dat.dis <- read.dta("NTL.dis.dst.dta")


##########################
###CW Onset replication###
##########################
#Model 1
log.FL.1.o.ds <- glm(onset ~ lnNL_sum_total + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                    logross_oil_prod + nwstate + instability + lagp_polity2 +
                    logcellarea + logttime + cluster(did) + factor(year) + factor(ccode),
                  data=dat.dis, family = "binomial")
summary(log.FL.1.o.ds)
AIC(log.FL.1.o.ds)
#Model 2
#subset the required data for ethnic war
dat.dis.eth <- dat.dis[ which(dat.dis$second > 0.04999),]
log.FL.2.o.ds <- glm(onset ~ lnNL_sum_total + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                    logross_oil_prod + nwstate + instability + lagp_polity2 +
                    logcellarea + logttime + cluster(did) + factor(year) + factor(ccode),
                  data=dat.dis.eth, family = "binomial")
summary(log.FL.2.o.ds)
AIC(log.FL.2.o.ds)
#Model 3
log.FL.3.o.ds <- glm(onset ~ lnNL_sum_total + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                    logross_oil_prod + nwstate + instability + 
                    laganocracy + lagpolbin + logcellarea + logttime + cluster(did) + factor(year) + factor(ccode),
                  data=dat.dis, family = "binomial")
summary(log.FL.3.o.ds)
AIC(log.FL.3.o.ds)

#Export to latex
stargazer(log.FL.1.o.ds, log.FL.2.o.ds, log.FL.3.o.ds)



